<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : operatecache.class.php   FILE_PATH : \classes\libraries\operatecache.class.php
 * ....cache文件操作类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Wed Feb 20 11:19:25 CST 2008
 */

class OperateCache{
	/**
	 * 数据缓存目录
	 *
	 * @var string
	 */
	var $cath_path = "cache";
	/**
	 * 缓存文件名称
	 *
	 * @var string
	 */
	var $cache_file_name;
	/**
	 * 缓存数组名称
	 *
	 * @var string
	 */
	var $cache_var;
	/**
	 * 数据文件名
	 *
	 * @var string
	 */
	var $data_file_name;
	/**
	 * 缓存数据数据
	 *
	 * @var string[]
	 */
	var $cache_array;
	/**
	 * 缓存数据临时存放文件名
	 *
	 * @var string
	 */
	var $cache_tempfile_name;
	/**
	 * 语言包地区
	 *
	 * @var string
	 */
	var $lang_versionarea = "zh-cn";

	/**
	 * 缓存文件类型
	 *
	 * @var string
	 */
	var $data_file_type = "ini";
	/**
	 * 系统配置文件数组
	 *
	 * @var string[]
	 */
	var $web_config;

	/**
	 * 菜单的属性列表 
	 *
	 * @var string
	 */
	var $menu_attribute;
	/**
	 * 缓存文件编码
	 *
	 * @var string
	 */
	var $cache_charset = 'UTF-8';

	/**
	 * 类初始化
	 *
	 * @param string $configfilename 配置文件名称
	 * @param string $type 生成缓存文件的类型
	 * @return IniFileCache
	 */
	function OperateCacheFile($datafilename,$type = "ini",$file_charset = 'UTF-8'){
		//echo $datafilename;
		$this->data_file_type = $type;
		$this->cache_charset = strtoupper($file_charset);
		if ($type == "ini"){
			$ini_name = substr($datafilename,0,strpos($datafilename,"."));
			$this->data_file_name = $datafilename;
			$this->cache_file_name = $ini_name . "ini.cache.php";
			$this->cache_tempfile_name = $ini_name . time() . "ini.cache.php";
			$this->cache_var = "cache_" . $ini_name;
		}else if($type == "lang"){
			$this->lang_versionarea = $this->lang_versionarea?$this->lang_versionarea:'zh-cn';
			$this->cath_path = "cache/lang";
//			$this->data_file_name = $datafilename . ".xml";
			$this->data_file_name = $datafilename . ".php";
			$this->cache_file_name = $datafilename . "." . $this->lang_versionarea . ".cache.php";
			$this->cache_tempfile_name = $datafilename . time() . "." . $this->lang_versionarea . ".cache.php";
			$this->cache_var = "cache_lang";
		}else if(strstr($type,"menu")){
			$this->cath_path = "cache/menu";
			$this->data_file_name = $datafilename . ".php";
			$this->cache_file_name = $datafilename . "." . $this->lang_versionarea . ".cache.php";
			$this->cache_tempfile_name = $datafilename . time() . "." . $this->lang_versionarea . ".cache.php";
			$this->cache_var = "cache_" . $datafilename;
			$this->data_file_type = "menu";

		}

		/**
		 * 如果不存在配置文件缓存文件那么生成此文件
		 */
		if (!file_exists(BasePath . "/" . $this->cath_path . "/" . $this->cache_file_name)){
			$function_name = "_createCacheFile" . "_" . $this->data_file_type;
			$this->cache_array =  $this->$function_name();
		}else{
			$this->cache_array =  $this->_getCacheConfig();
		}
	}

	/**
	 * 比较缓存文件是否是最新的，如果不是生成，是的话读取缓存文件中的内容
	 *
	 * @return string[]
	 */
	function _getCacheConfig(){
		/**
		 * 引入配置文件内容
		 */
		require(BasePath . "/" . $this->cath_path . "/" . $this->cache_file_name);
		/**
		 * 得到配置文件最后一次修改时间
		 */
		if ($this->data_file_type == "ini" || strstr($this->data_file_type,"menu") != false){
			$filelasttime = @filemtime(BasePath . "/config/" . $this->data_file_name);
		}elseif ($this->data_file_type == "lang"){
			$filelasttime = @filemtime(BasePath . "/lang/" . $this->lang_versionarea . "/" . $this->data_file_name);
		}
		$var_name = $this->cache_var;
		$config_array = $$var_name;

		if ($config_array['_sys_']['lasttime'] < $filelasttime){
			$function_name = "_createCacheFile" . "_" . $this->data_file_type;
			return $this->$function_name();
		}else{
			return $config_array;
		}
	}


	/**
	 * 创建配置文件缓存目录
	 *
	 * @return string[]
	 */
	function _createCacheFile_ini(){
			
		/**
		 * 得到程序系统配置信息
		 */
		require_once(BasePath . "/classes/libraries/inifileoperate.class.php");
		$objini = new IniFileOperate(BasePath . "/config/" . $this->data_file_name);
		$configinfosection = $objini->sections();
		foreach($configinfosection as $k=>$v){
			$iniinfo[$v] = $objini->keys($v);
		}
		/**
		 * 建立临时缓存文件
		 */
		if (file_exists(BasePath . "/cache/" . $this->cache_tempfile_name)){
			@unlink(BasePath . "/cache/" . $this->cache_tempfile_name);
		}
		$fp = @fopen(BasePath . "/cache/" . $this->cache_tempfile_name, "w+");
		@flock($fp,2);
		@chmod(BasePath . "/cache/" . $this->cache_tempfile_name,0777);
		@fputs($fp, "<?php\n");
		@fputs($fp, "\$" . $this->cache_var . "['_sys_']['lasttime'] = " . time() . ";\n");
		foreach ($iniinfo as $k=>$v){
			foreach($v as $subk=>$subv){
				if ($k != "" && $subk !="" && $subk != "?>"){
					if($subk == 'ncharset'){
						$this->cache_charset = $subv;
					}
					//判断是否是数据库密码，如果是，则清除左右双引号
					if ($subk == 'password_write' || $subk == 'username_write' || $subk == 'password_read' || $subk == 'username_read'){
						$subv = trim($subv,'"');
					}
					if($this->cache_charset == 'GBK'){
						if ($this->lang_versionarea == "zh") {
							$tmp_subv = mb_convert_encoding(addslashes($subv), "GBK", "UTF-8");
						}
						else {
							$tmp_subv = Common::nc_change_charset($subv,'utf8_to_gbk');
						}
					}else{
						$tmp_subv = addslashes($subv);
					}
					@fputs($fp, "\$" . $this->cache_var . "['" . $k . "']['" . $subk . "']=\"" . $tmp_subv . "\";\n");
				}
			}
		}
		@fputs($fp, "?>");
		@pclose($fp);
		/**
		 * 删除旧的缓存文件
		 */
		if (@file_exists(BasePath . "/cache/" . $this->cache_file_name)){
			@unlink(BasePath . "/cache/" . $this->cache_file_name);
		}
		@rename(BasePath . "/cache/" . $this->cache_tempfile_name,BasePath . "/cache/" . $this->cache_file_name);
		$objini = null;
		//重新载入缓存文件
		require(BasePath . "/cache/" . $this->cache_file_name);
		$cache_var = $this->cache_var;
		$iniinfo = $$cache_var;
		return $iniinfo;
	}

	/**
	 * 创建语言包文件缓存目录
	 *
	 * @return string[]
	 */
	function _createCacheFile_lang(){
		//获取文件内容 $lang 为内容
		include_once(BasePath . "/lang/" . $this->lang_versionarea . "/" . $this->data_file_name);
		if ($lang != ''){
			//写入缓存文件
			$cache_file = BasePath . "/" . $this->cath_path . "/" . $this->cache_file_name;
			if ($fp = @fopen($cache_file, 'wb+')) {
				@fputs($fp, "<?php\n");
				@fputs($fp, "\$" . $this->cache_var . "['_sys_']['lasttime'] = " . time() . ";\n");
				foreach ($lang as $k => $v){
					if($this->cache_charset == 'GBK'){
						if ($this->lang_versionarea == "zh") {
							$v = str_replace("\\","\\\\",mb_convert_encoding($v, "GBK", "UTF-8"));
						}
						else {
							$v = Common::nc_change_charset($v,'utf8_to_gbk');
						}
					}
					@fputs($fp, "\$" . $this->cache_var . "['" . $k . "']=\"" . addslashes($v) . "\";\n");
					$lang[$k] = $v;
				}
				@fputs($fp, "?>");
			}
			return $lang;
		}else {
			return false;
		}
		//============================================
//		if ($this->web_config == ""){
//			return false;
//		}
		
		/**
		 * 得到语言包信息
		
		$hdc = new XmlParse(BasePath . "/lang/" . $this->lang_versionarea . "/" . $this->data_file_name);
		$data_count = $hdc->count_tag("root:data","1:?");
		for($i=1;$i<=$data_count;$i++){
			$k = $hdc->get_attribute("root:data","1:" . $i,"name");
			$tmp_v = $hdc->get_tag_text("root:data:value","1:" . $i . ":1");
			
			if($this->cache_charset == 'GBK'){
//				if(function_exists('iconv') && ICONV_VERSION < 2){
//					$v = iconv("UTF-8", "GBK//IGNORE", $tmp_v);
//				}else{
				if ($this->lang_versionarea == "zh") {
					$v = str_replace("\\","\\\\",mb_convert_encoding($tmp_v, "GBK", "UTF-8"));
				}
				else {
					$v = Common::nc_change_charset($tmp_v,'utf8_to_gbk');
				}			
					
//				}
			}else{
				$v = $tmp_v;
			}
			
			foreach($this->web_config as $key=>$value){
				if ($key!=""){
					$v = str_replace('$' . $key,$value,$v);
				}
			}
			$lang[$k] = $v;
		}
		unset($hdc);
 */

		/**
		 * 建立临时缓存文件
		 
		if (@file_exists(BasePath . "/" . $this->cath_path . "/" . $this->cache_tempfile_name)){
			@unlink(BasePath . "/" . $this->cath_path . "/" . $this->cache_tempfile_name);
		}
		$fp = @fopen(BasePath . "/" . $this->cath_path . "/" . $this->cache_tempfile_name, "w+");

		@flock($fp,2);
		@chmod(BasePath . "/" . $this->cath_path . "/" . $this->cache_tempfile_name,0777);
		@fputs($fp, "<?php\n");
		@fputs($fp, "\$" . $this->cache_var . "['_sys_']['lasttime'] = " . time() . ";\n");
		foreach ($lang as $k=>$v){
			@fputs($fp, "\$" . $this->cache_var . "['" . $k . "']=\"" . addslashes($v) . "\";\n");
		}
		@fputs($fp, "?>");
		@pclose($fp);*/
		/**
		 * 删除旧的缓存文件
		
		if (file_exists(BasePath . "/" . $this->cath_path . "/" . $this->cache_file_name)){
			@unlink(BasePath . "/" . $this->cath_path . "/" . $this->cache_file_name);
		}
		@rename(BasePath . "/" . $this->cath_path . "/" . $this->cache_tempfile_name,BasePath . "/" . $this->cath_path . "/" . $this->cache_file_name);
		$objini = null;
		//重新载入缓存文件
		require(BasePath . "/" . $this->cath_path . "/" . $this->cache_file_name);
		$cache_var = $this->cache_var;
		$iniinfo = $$cache_var;
		return $iniinfo; */
	}

	function _createCacheFile_menu(){
		
		//初始化系统
		if(!is_object($this->obj_frameword)){
			require_once("framework.core.php");
			$this->obj_frameword = new FrameWork();
		}
		$this->obj_frameword->setFrameWork();
		$this->obj_frameword->_langType = $this->lang_versionarea;
		$this->obj_frameword->getlang("menu");
		
//		$cur_url = Common::getCurrUrl();
		$menu_array = array();
		require(BasePath . "/config/" . $this->data_file_name);
		$data_count = count($menu);
		for($i=1;$i<=$data_count;$i++){
			$k = $i;
			foreach($this->menu_attribute[0] as $v){
				$menu[$k][$v] = $this->_replaceMenuLang($menu[$k][$v], $this->lang_versionarea);
			}

			$data_count_sub = count($menu[$i]['baby']);

			if ($data_count_sub <1 ){
				$menu[$k]['name'] = $this->_replaceMenuLang($menu[$k]['name'], $this->lang_versionarea);
				
			}else{
				for($j=1;$j<=$data_count_sub;$j++){
					$menu[$k]['baby'][$j]['name'] = $this->_replaceMenuLang($menu[$k]['baby'][$j]['name'], $this->lang_versionarea);
				}
			}
		}

		unset($hdc);


		/**
		 * 建立临时缓存文件
		 */
		if (@file_exists(BasePath . "/" . $this->cath_path . "/" . $this->cache_tempfile_name)){
			@unlink(BasePath . "/" . $this->cath_path . "/" . $this->cache_tempfile_name);
		}
		$fp = @fopen(BasePath . "/" . $this->cath_path . "/" . $this->cache_tempfile_name, "w+");
		@flock($fp,2);
		@chmod(BasePath . "/" . $this->cath_path . "/" . $this->cache_tempfile_name,0777);
		@fputs($fp, "<?php\n");
		@fputs($fp, "\$" . $this->cache_var . "['_sys_']['lasttime'] = " . time() . ";\n");

		foreach ($menu as $k=>$v){
			foreach ($v as $firstattrk=>$firstattrv){
				if (!is_array($firstattrv)){
					@fputs($fp, "\$" . $this->cache_var . "[" . $k . "]['" . $firstattrk . "']=\"" . trim(addslashes($firstattrv)) . "\";\n");
				}else{
					foreach ($firstattrv as $subk=>$subv){
						foreach ($subv as $secondk=>$secondv){
							@fputs($fp, "\$" . $this->cache_var . "[" . $k . "]['baby'][" . $subk . "]['" . $secondk . "']=\"" . trim(addslashes($secondv)) . "\";\n");
						}
					}
				}
			}
		}
		@fputs($fp, "?>");
		@pclose($fp);
		/**
		 * 删除旧的缓存文件
		 */
		if (@file_exists(BasePath . "/" . $this->cath_path . "/" . $this->cache_file_name)){
			@unlink(BasePath . "/" . $this->cath_path . "/" . $this->cache_file_name);
		}
		@rename(BasePath . "/" . $this->cath_path . "/" . $this->cache_tempfile_name,BasePath . "/" . $this->cath_path . "/" . $this->cache_file_name);
		$objini = null;
		
		//重新载入缓存文件
		require(BasePath . "/" . $this->cath_path . "/" . $this->cache_file_name);
		$cache_var = $this->cache_var;
		$menu = $$cache_var;
		
		return $menu;

	}

	/**
	 * 替换目录中的语言包文件
	 * @author workflow Develop Team     
	 * @param  
	 * @return int/bool/object/array/string
	 */
	function _replaceMenuLang($langString,$langType="zh-cn"){
		if (preg_match_all("|\{lang(.*)\}|U", $langString, $matches, PREG_SET_ORDER)){
			foreach ($matches as $key => $values){
				$langReplaceString[$key] = preg_replace("|\{lang{$values['1']}\}|U", $this->obj_frameword->_lang["lang{$values['1']}"], $langString);
			}
		}
		if(is_array($langReplaceString)){
			$langString = join($langReplaceString);
		}
		return $langString;
	}
	
}
?>